# -*- coding: latin-1 -*-
# Copyright (c) 2007-2009 Jorge Ortiz Romo <jortizromo@gmail.com>
#                         Freymam Vallejo Cuero <freturn@gmail.com>
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# Import the module and instantiate a pygraph object

'''
Created on Dec 7, 2009

@author: Freymam Vallejo
@author: Jorge Ortiz
'''

from __future__ import division
from iasna_core.sna.common import Matrix
import Degree

# ---------------------------------
# Funciones para Centralidad de grado
# ---------------------------------

def degree_centrality(digraph, ni):
    """
    It calculates Degree Centrality based on outdegree for a node .

    @type digraph: Digraph.
    @param digraph: Digraph.
    
    @type char: Node.
    @param ni: Node.

    @rtype: number.
    @return: Degree Centrality value for ni
    """
    
    return Degree.digraph_outdegree(digraph, ni)
    

def degree_centrality_proportion(digraph, ni):
    """
    It calculates Outdegree AVG of a node ni in respect of the size of the digraph - 1, or in other words the AVG of salient relations in respect of the other nodes.

    @type digraph: Digraph.
    @param digraph: Digraph.
    
    @type char: Node.
    @param ni: Node.

    @rtype: number.
    @return: Outdegree AVG of ni node.
    """
    
    centrality = degree_centrality(digraph, ni)
    g_1 = len (digraph.nodes()) - 1
    centrality_pro = centrality / g_1
    
    return centrality_pro


def group_degree_centrality(digraph):
    """
    It calculates Degree Centrality for the entire Network

    @type digraph: Digraph.
    @param digraph: Digraph.

    @rtype: number.
    @return: Network Degree Centrality value.
    """
    
    cdn_cdn = 0
    g_1 = len (digraph.nodes())
    centralities = []
    gro_centrality = 0
    
    if g_1 > 0:
        for node in digraph.nodes():
            centralities.append (degree_centrality(digraph, node))
        largest_centrality = max (centralities)
        
        
        for centrality in centralities:
            cdn_cdn += largest_centrality - centrality 
        
        gro_centrality = cdn_cdn / pow (g_1, 2)
    
    return gro_centrality


def group_degree_centrality_stdev(digraph):
    """
    it calculates the standard deviation for all the network degree centrality.

    @type digraph: Digraph.
    @param digraph: Digraph.

    @rtype: number.
    @return: stdev network degree centrality value.
    """
    
    g = len (digraph.nodes())
    centralities = []
    cd = 0
    cdn_cd = 0
    
    for node in digraph.nodes():
        cen = degree_centrality (digraph, node)
        centralities.append (cen)
        cd += cen
    cd /= g    
    
    for centrality in centralities:
        cdn_cd += pow (centrality - cd, 2)
    centrality_stdev = cdn_cd / g
    
    return centrality_stdev
